package com.tdkj.service.shop.impl;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.dao.shop.IBeanCreditRatingDao;
import com.tdkj.model.shop.BeanCreditRating;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.shop.IBeanCreditRatingService;
@Component
public class BeanCreditRatingServiceImpl extends BaseService implements
		IBeanCreditRatingService {

	private  IBeanCreditRatingDao beanCreditRatingDao;
    
	@Resource
	public void setBeanCreditRatingDao(IBeanCreditRatingDao beanCreditRatingDao) {
		this.beanCreditRatingDao = beanCreditRatingDao;
	}

	public IBeanCreditRatingDao getBeanCreditRatingDao() {
		return beanCreditRatingDao;
	}

	@Override
	public BeanCreditRating addCreditRating(String name,
			Integer scoreIntervalLower, Integer scoreIntervalUpper, String imgUrl, Integer ordinal) {
		BeanCreditRating bean = new BeanCreditRating();
		bean.setImgUrl(imgUrl);
		bean.setName(name);
		bean.setScoreIntervalLower(scoreIntervalLower);
		bean.setScoreIntervalUpper(scoreIntervalUpper);
		bean.setOrdinal(ordinal);
		beanCreditRatingDao.save(bean);
		
		return bean;
	}

	@Override
	public void deleteCreditRating(Long id) {
		beanCreditRatingDao.delete(id, BeanCreditRating.class);
	}

	@Override
	public List<BeanCreditRating> getAllCreditRatings() {
		LinkedHashMap<String,String> orderInfos = new LinkedHashMap<String,String>();
		orderInfos.put("id", "asc");
		return beanCreditRatingDao.findAll(BeanCreditRating.class, orderInfos);
	}

	@Override
	public BeanCreditRating getCreditRating(Long id) {
		return beanCreditRatingDao.findById(id, BeanCreditRating.class);
	}

	@Override
	public void updateCreditRating(Long id, String name,
			Integer scoreIntervalLower, Integer scoreIntervalUpper, String imgUrl,
			Integer ordinal) {
		BeanCreditRating bean = beanCreditRatingDao.findById(id, BeanCreditRating.class);
		bean.setName(name);
		bean.setImgUrl(imgUrl);
		bean.setScoreIntervalLower(scoreIntervalLower);
		bean.setScoreIntervalUpper(scoreIntervalUpper);
		bean.setOrdinal(ordinal);
	}

	@Override
	public void updateUser(Long userId) {
		BeanUser user = beanCreditRatingDao.findById(userId, BeanUser.class);
		String hql = " from " + BeanCreditRating.class.getName() + " as bean where bean.scoreIntervalLower<='"+user.getCreditScore()+"' and bean.scoreIntervalUpper>='"+user.getCreditScore()+"'";
		BeanCreditRating creditRating = (BeanCreditRating)beanCreditRatingDao.getSession().createQuery(hql).uniqueResult();
		user.setCreditRating(creditRating);
	}

	@Override
	public void updateShop(Long shopId) {
		BeanShop shop = beanCreditRatingDao.findById(shopId, BeanShop.class);
		String hql = " from " + BeanCreditRating.class.getName() + " as bean where bean.scoreIntervalLower<='"+shop.getCreditScore()+"' and bean.scoreIntervalUpper>='"+shop.getCreditScore()+"'";
		BeanCreditRating creditRating = (BeanCreditRating)beanCreditRatingDao.getSession().createQuery(hql).uniqueResult();
		shop.setCreditRating(creditRating);
	}
	
}
